package org.sdnstproject.interlayer.OF13Protocal;

import java.util.Random;

import org.projectfloodlight.openflow.types.U16;
import org.projectfloodlight.openflow.types.U32;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;

public class FuDescStatsReply {
	private static final Logger log = LoggerFactory.getLogger(FuDescStatsReply.class);
    public byte version = (byte)4;
    public byte type = (byte)26;
    public short length = (short)1072;// fixed value property length = 1072
    public long xid = 0x0L;
    public short statsType = (short)0;// fixed value property statsType = 0
    public int flags =(short) 0x1;
    public byte[] mfrDesc = new byte[256];//256 String
    public byte[] hwDesc = new byte[256];//256 String
    public byte[] swDesc = new byte[256];//256 String
    public byte[] serialNum = new byte[32];//32 String
    public byte[] dpDesc = new byte[256];//256 String
	
	public FuDescStatsReply() {
		
	}
	
	public FuDescStatsReply setXid(long xid) {
		this.xid = xid;
		return this;
	}
	
	public FuDescStatsReply setField(String field, String value) {
		switch (field) {
		case "version":
			this.version = Byte.parseByte(value);
			break;
		case "type":
			this.type = Byte.parseByte(value);
			break;
		case "length":
			this.length = Short.parseShort(value);
			break;
		case "xid":
			this.xid = Long.parseLong(value);
			break;
		case "statsType":
			this.statsType = Short.parseShort(value);
			break;
		case "flags":
			this.flags = Integer.parseInt(value);
			break;
		case "mfrDesc":
			this.mfrDesc = new byte[256];//TODO String
			new Random().nextBytes(mfrDesc);
			break;
		case "hwDesc":
			this.hwDesc = new byte[256];//TODO String
			new Random().nextBytes(hwDesc);
			break;
		case "swDesc":
			this.swDesc = new byte[256];//TODO String
			new Random().nextBytes(swDesc);
			break;
		case "serialNum":
			this.serialNum = new byte[32];//TODO String
			new Random().nextBytes(serialNum);
			break;
		case "dpDesc":
			this.dpDesc = new byte[256];//TODO String
			new Random().nextBytes(dpDesc);
			break;
		}
		return this;
	}
	
	public ByteBuf writeTo() {
		ByteBuf bb = Unpooled.directBuffer();
		// fixed value property version = 4
        bb.writeByte(version);
        // fixed value property type = 19
        bb.writeByte(type);
        // fixed value property length = 1072
        bb.writeShort(length);
        bb.writeInt(U32.t(xid));
        // fixed value property statsType = 0
        bb.writeShort(statsType);
        bb.writeShort(U16.t(flags));
        // pad: 4 bytes
        bb.writeZero(4);
        bb.writeBytes(mfrDesc);
        bb.writeBytes(hwDesc);
        bb.writeBytes(swDesc);
        bb.writeBytes(serialNum);
        bb.writeBytes(dpDesc);
        return bb;
    }
	
	@Override
    public String toString() {
        StringBuilder b = new StringBuilder("OFDescStatsReplyVer13(");
        b.append("version=").append(version);
        b.append(", ");
        b.append("type=").append(type);
        b.append(", ");
        b.append("length=").append(length);
        b.append(", ");
        b.append("xid=").append(xid);
        b.append(",");
        b.append("flags=").append(flags);
        b.append(", ");
        b.append("mfrDesc=").append(mfrDesc);
        b.append(", ");
        b.append("hwDesc=").append(hwDesc);
        b.append(", ");
        b.append("swDesc=").append(swDesc);
        b.append(", ");
        b.append("serialNum=").append(serialNum);
        b.append(", ");
        b.append("dpDesc=").append(dpDesc);
        b.append(")");
        return b.toString();
    }
}
